package net.sky.oa.service.ioffice.impl;


import java.util.Set;
import java.util.Date;
import java.util.List;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Collection;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.htsoft.core.service.impl.BaseServiceImpl;
import com.htsoft.core.web.paging.PagingBean;

import net.sky.oa.dao.ioffice.OffSummaryDao;
import net.sky.oa.model.OffSummUser;
import net.sky.oa.model.OffSummary;
import net.sky.oa.model.SysEmployee;
import net.sky.oa.service.ioffice.OffSummaryService;
import net.sky.oa.util.ReflectionUtils;

@Service
@Transactional
public class OffSummaryServiceImpl extends BaseServiceImpl<OffSummary> implements
		OffSummaryService {
	
	private OffSummaryDao offSummaryDao;
	
	@Resource
	public void setOffSummaryDao(OffSummaryDao offSummaryDao) {
		super.setDao(offSummaryDao);
		this.offSummaryDao = offSummaryDao;
	}
	

	/**
	 * 作用：
	 * 1、把原来选中现在选中的保留
	 * 2、把原来选中现在没有选中的删除掉
	 * 3、把原来没有选中现在选中的增加进去
	 * @param checkedIds
	 */
	//@Transactional(readOnly=true)
	public void mergeByCheckedIds(OffSummary entity,final Collection<Long> checkedIds) {
		
		Set<OffSummUser> summUsers = entity.getOffSummUsers();
		
		if(summUsers == null){
			summUsers = new HashSet<OffSummUser>();
		}

		//目标集合为空, 删除源集合中所有对象后直接返回.
		System.out.println(checkedIds);
		if (checkedIds == null) {
			//summUsers.clear();
			Iterator<OffSummUser> it = summUsers.iterator();
			try {
				while (it.hasNext()) { 
					it.next();
					it.remove();
				}
				entity.setOffSummUsers(null);
			return;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		//遍历源对象集合,如果其id不在目标ID集合中的对象删除.
		//同时,在目标集合中删除已在源集合中的id,使得目标集合中剩下的id均为源集合中没有的id.
		Iterator<OffSummUser> it = summUsers.iterator();
		try {

			while (it.hasNext()) {
				OffSummUser element = it.next();
				Long id = element.getRusr().getId();

				if (!checkedIds.contains(id)) {
					it.remove();
				} else {
					checkedIds.remove(id);
				}
			}

			//ID集合目前剩余的id均不在源集合中,创建对象,为id属性赋值并添加到源集合中.
			for (Long id : checkedIds) {
				OffSummUser summUser = new OffSummUser();
				
				SysEmployee user = new SysEmployee();
				user.setId(id);		
				summUser.setRusr(user);
				summUser.setSumm(entity);
				summUser.setIsread(1);
				summUser.setSendtime(new Date());
				
				summUsers.add(summUser);
			}
			entity.setOffSummUsers(summUsers);
		} catch (Exception e) {
			e.printStackTrace();
			throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
		}
	}
	
	public List<OffSummary> findByRusr(int flag,Long rusrId,HttpServletRequest getRequest,PagingBean pb)
	{
		String title=getRequest.getParameter("Q_title_S_LK");
		String type=getRequest.getParameter("Q_type_N_EQ");
		String sort=getRequest.getParameter("Q_sort_N_EQ");
		String time=getRequest.getParameter("Q_lasttime_D_EQ");
		int ty=-1;
		int so=-1;
		Date lasttime = null;
		if(StringUtils.isNotEmpty(type))ty=Integer.valueOf(type);
		if(StringUtils.isNotEmpty(sort))ty=Integer.valueOf(so);
		try {
			if(StringUtils.isNotEmpty(time))
			lasttime = new SimpleDateFormat("yyyy-MM-dd").parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return this.offSummaryDao.findByRusr(flag,rusrId, title, ty, so, lasttime, pb);
	}
	
	public OffSummary findById(Long id,Long rusrId,String sort,PagingBean pb)
	{
		return this.offSummaryDao.findById(id, rusrId, sort, pb);
	}
}
